package com.motorhome.manage.service.impl;

import com.motorhome.base.util.DigestUtil;
import com.motorhome.base.util.HttpClientUtil;
import com.motorhome.base.util.JsonUtil;
import com.motorhome.manage.common.statics.ManageStatics;
import com.motorhome.manage.service.OpenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: John
 */
@Service
public class OpenServiceImpl implements OpenService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private String post(String url, String data) {
        String openId = ManageStatics.OPEN_ID;
        String secret = ManageStatics.OPEN_SECRET;
        String timestamp = System.currentTimeMillis() + "";
        String signature = DigestUtil.md5(openId + secret + timestamp + data).toUpperCase();

        Map<String, Object> requestParams = new HashMap<>();
        requestParams.put("data", data);

        return new HttpClientUtil().post(
                url + "?openId=" + openId + "&signature=" + signature + "&timestamp=" + timestamp,
                requestParams);
    }

    @Override
    public String updateMemberPoint(String userAccount, String cardId, Float point, String meno) {
        Map<String, Object> data = new HashMap<>();
        data.put("userAccount", userAccount);
        data.put("cardId", cardId);
        data.put("point", point);
        data.put("meno", meno);
        return post(ManageStatics.UPDATE_MEMBERPOINT_URL, JsonUtil.toJson(data));
    }
}
